/*---------------------------------------------------------------------------------

	A very simple tiled background...no tools required

---------------------------------------------------------------------------------*/
#include <nds.h>

//a tiled background needs a map to tell the DS how to arange the tiles on the screen
//here is our hand drawn map.  Notice it is 16x11 tiles which wont quite line up to a 
//native DS hardware map size
u16 map[] = {
01,01,01,01,01,01,01,00,00,01,01,01,01,01,01,01,
01,01,01,01,02,01,01,00,00,01,01,01,01,01,01,01,
01,01,01,01,00,00,00,00,00,01,01,01,01,01,01,01,
01,01,01,00,00,00,00,00,00,01,01,01,01,01,01,01,
01,01,00,00,00,00,00,00,00,00,01,01,01,01,01,01,
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,
01,01,00,00,00,00,00,00,00,00,00,00,00,00,01,01,
01,01,00,00,00,00,00,00,00,00,00,00,00,00,01,01,
01,01,00,00,00,00,00,00,00,00,00,00,00,00,01,01,
01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,
01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01
};

//We of coure need some tiles...here are three simple solid color tiles.
//Looking at the map above we want tile 0 to be the tan ground color, tile 1
//to be the green rock color and tile 2 to be the black stairwell.
//We set the actual colors for these pixels in the main code.
u8 tiles[] = {
	//tile 00 we want tan
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	//tile 01 we want green
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	//tile 02 we want black
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
};
//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	//set up mode 0 and map some video memory to our background engine
	videoSetMode(MODE_0_2D);
	vramSetBankA(VRAM_A_MAIN_BG);

	//initialize a tiled background.  A "text" background thats 256x256
	int bg = bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 0, 1);

	//Here is where we set the colors for our solid colored tiles
	//notice tile 00 was filled with color index 1 and since it is 
	//our tan tile we set palette index 1 to tan...the others follow 
	BG_PALETTE[0] = RGB15(0,0,0); //black
	BG_PALETTE[1] = RGB15(31,27,20); //tan
	BG_PALETTE[2] = RGB15(10,27,8); //green

	//first lets copy our tiles into tile memory
	dmaCopy(tiles, bgGetGfxPtr(bg), sizeof(tiles));
	
	//lets get a pointer to our hardware map so we can copy the one we made in
	u16* m		= bgGetMapPtr(bg);

	//now we copy in our map...a little bit tricky because i was too lazy to make
	//a 32x32 map.  The (x/2) + (y/2) causes us to write the same tile twice
	for(int x = 0; x < 32; x++)
		for(int y = 0; y < 22; y++)
		{
			m[x + y * 32] = map[(x/2) + (y/2) * 16];
		}

	while(1)
	{
		swiWaitForVBlank();
	}

}
